﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using System.Data.SqlClient;
using System.Data;
using BaHuan.Entities;

namespace BaHuan.DataLayer
{
    class TaiKhoanGiaoDichDoiTacDAO:BaseDAO
    {
        // save into DB, @id = -1: insert else, update
        public ResultObj SaveTaiKhoanGiaoDich(TaiKhoanGiaoDichDoiTac taikhoangiaodichdoitac)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            SqlConnection conObj = DataConnection.GetSqlConnection();
            SqlCommand cmd = CreateSqlCommand("spAddTaiKhoanGiaoDichDoiTac", CommandType.StoredProcedure);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "spAddTaiKhoanGiaoDichDoiTac";
            SqlParameter param = new SqlParameter();
            param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, taikhoangiaodichdoitac.Id);
            cmd.Parameters.Add(param);
            param = CreateParameter("@IdNganHang", DbType.Int32, ParameterDirection.Input, taikhoangiaodichdoitac.IdNganHang);
            cmd.Parameters.Add(param);
            param = CreateParameter("@TenGiaoDich", DbType.String, ParameterDirection.Input, taikhoangiaodichdoitac.TenGiaoDich);
            cmd.Parameters.Add(param);
            param = CreateParameter("@SoTaiKhoanGiaoDich", DbType.String, ParameterDirection.Input, taikhoangiaodichdoitac.SoTaiKhoanGiaoDich);
            cmd.Parameters.Add(param);
            param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
            cmd.Parameters.Add(param);
            conObj.Open();
            cmd.ExecuteNonQuery();
            conObj.Close();
            result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);

            return result;
        }

        // Get all TaiKhoanGiaoDich in DB
        public ResultObj GetAllTaiKhoanGiaoDichDoiTac()
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<TaiKhoanGiaoDichDoiTac> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetAllTaiKhoanGiaoDichDoiTac", CommandType.StoredProcedure);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<TaiKhoanGiaoDichDoiTac>();
                TaiKhoanGiaoDichDoiTac item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new TaiKhoanGiaoDichDoiTac();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.IdNganHang = Convert.ToInt32(reader["IdNganHang"]);
                    item.TenGiaoDich = Convert.ToString(reader["TenGiaoDich"]);
                    item.SoTaiKhoanGiaoDich = Convert.ToString(reader["SoTaiKhoanGiaoDich"]);
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }

        // Get all TaiKhoanGiaoDich in DB with condition
        public ResultObj GetAllTaiKhoanGiaoDichDoiTacByID(TaiKhoanGiaoDichDoiTac taikhoangiaodichdoitac)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<TaiKhoanGiaoDichDoiTac> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetAllTaiKhoanGiaoDichDoiTacByID", CommandType.StoredProcedure);
            SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, taikhoangiaodichdoitac.Id);
            cmd.Parameters.Add(param);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<TaiKhoanGiaoDichDoiTac>();
                TaiKhoanGiaoDichDoiTac item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new TaiKhoanGiaoDichDoiTac();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.IdNganHang = Convert.ToInt32(reader["IdNganHang"]);
                    item.TenGiaoDich = Convert.ToString(reader["TenGiaoDich"]);
                    item.SoTaiKhoanGiaoDich = Convert.ToString(reader["SoTaiKhoanGiaoDich"]);
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }

        // Delete a TaiKhoanGiaoDich out of DB with condition
        public ResultObj DeleteTaiKhoanGiaoDichDoiTac(TaiKhoanGiaoDichDoiTac taikhoangiaodichdoitac)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            SqlConnection conObj = DataConnection.GetSqlConnection();
            SqlCommand cmd = CreateSqlCommand("spDeleteTaiKhoanGiaoDichDoiTac", CommandType.StoredProcedure);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "spDeleteTaiKhoanGiaoDichDoiTac";
            SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, taikhoangiaodichdoitac.Id);
            cmd.Parameters.Add(param);
            conObj.Open();
            result.ReturnCode = cmd.ExecuteNonQuery();
            conObj.Close();

            return result;
        }

        // Get info of NganHang from DB with condition
        public ResultObj GetNganHang(int IdNganHang)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<NganHang> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetNganHang", CommandType.StoredProcedure);
            SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdNganHang);
            cmd.Parameters.Add(param);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<NganHang>();
                NganHang item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new NganHang();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.MaSo = Convert.ToString(reader["MaSo"]);
                    item.Ten = Convert.ToString(reader["Ten"]);
                    item.DiaChi = Convert.ToString(reader["DiaChi"]);
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }
    }
}
